<!DOCTYPE html>
<html lang="zh-Hans">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="golang 中自带时间处理的包,然而对之前写php的程序员来说,用法上依然有很多很繁琐和不适应的地方."/>




  <meta name="keywords" content="golang, 技术, 杂鱼君的博客" />










  <link rel="alternate" href="/atom.xml" title="杂鱼君的博客">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="http://notes.wen.moe/post/golang-time-package.html"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>









    <title> phper 学 golang --日期时间处理 - 杂鱼君的博客 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">杂鱼君的博客</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/">
        <li class="mobile-menu-item">
          
          
            Home
          
        </li>
      </a>
    
      <a href="/archives/">
        <li class="mobile-menu-item">
          
          
            Archives
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            Tags
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            About
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">杂鱼君的博客</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/">
            
            
              Home
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives/">
            
            
              Archives
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              Tags
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              About
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          phper 学 golang --日期时间处理
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2016-07-18
        </span>
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">Contents</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#时间相关的常量"><span class="toc-text">时间相关的常量</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#格式化时间"><span class="toc-text">格式化时间</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#基础的时间常量"><span class="toc-text">基础的时间常量</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#字符串和时间戳"><span class="toc-text">字符串和时间戳</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#获取当前时间戳"><span class="toc-text">获取当前时间戳</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#通过给定字符串获取时间戳"><span class="toc-text">通过给定字符串获取时间戳</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#通过给定时间戳获取时间字符串"><span class="toc-text">通过给定时间戳获取时间字符串</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#程序暂停一段时间"><span class="toc-text">程序暂停一段时间</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <h3 id="时间相关的常量"><a href="#时间相关的常量" class="headerlink" title="时间相关的常量"></a>时间相关的常量</h3><h4 id="格式化时间"><a href="#格式化时间" class="headerlink" title="格式化时间"></a>格式化时间</h4><p>golang 中时间处理的包叫<code>time</code>,直接导入就可以使用.初上手的人一定会对其中指定的时间格式字串感到崩溃,<br><code>2006-01-02 15:04:05</code>这么魔幻的时间到底该怎么记?有人(就是我)提出一套口诀:</p>
<figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span></div><div class="line"></div><div class="line">月日时分秒年区 (区代表时区)</div></pre></td></tr></table></figure>
<p>背吧,也不难.习惯了也没啥了.golang 的核心开发者很陶醉于这种特立独行的方式,但是我对此嗤之以鼻,根本没有传统的<code>Y-m-d H:i:s</code>好用.</p>
<p>不过你也不需要记,<code>time</code>包中已经预设了常见的格式化用的常量了:</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">time.ANSIC       = <span class="string">"Mon Jan _2 15:04:05 2006"</span></div><div class="line">time.UnixDate    = <span class="string">"Mon Jan _2 15:04:05 MST 2006"</span></div><div class="line">time.RubyDate    = <span class="string">"Mon Jan 02 15:04:05 -0700 2006"</span></div><div class="line">time.RFC822      = <span class="string">"02 Jan 06 15:04 MST"</span></div><div class="line">time.RFC822Z     = <span class="string">"02 Jan 06 15:04 -0700"</span> <span class="comment">// RFC822 with numeric zone</span></div><div class="line">time.RFC850      = <span class="string">"Monday, 02-Jan-06 15:04:05 MST"</span></div><div class="line">time.RFC1123     = <span class="string">"Mon, 02 Jan 2006 15:04:05 MST"</span></div><div class="line">time.RFC1123Z    = <span class="string">"Mon, 02 Jan 2006 15:04:05 -0700"</span> <span class="comment">// RFC1123 with numeric zone</span></div><div class="line">time.RFC3339     = <span class="string">"2006-01-02T15:04:05Z07:00"</span></div><div class="line">time.RFC3339Nano = <span class="string">"2006-01-02T15:04:05.999999999Z07:00"</span></div><div class="line">time.Kitchen     = <span class="string">"3:04PM"</span></div><div class="line"><span class="comment">// Handy time stamps.</span></div><div class="line">time.Stamp      = <span class="string">"Jan _2 15:04:05"</span></div><div class="line">time.StampMilli = <span class="string">"Jan _2 15:04:05.000"</span></div><div class="line">time.StampMicro = <span class="string">"Jan _2 15:04:05.000000"</span></div><div class="line">time.StampNano  = <span class="string">"Jan _2 15:04:05.000000000"</span></div></pre></td></tr></table></figure>
<h4 id="基础的时间常量"><a href="#基础的时间常量" class="headerlink" title="基础的时间常量"></a>基础的时间常量</h4><p>golang 中基础时间单位是<code>Nanosecond</code>,为了方便计算,<code>time</code>包中预设了如下几个常量:</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">time.Nanosecond           = <span class="number">1</span></div><div class="line">time.Microsecond          = <span class="number">1000</span> * Nanosecond</div><div class="line">time.Millisecond          = <span class="number">1000</span> * Microsecond</div><div class="line">time.Second               = <span class="number">1000</span> * Millisecond</div><div class="line">time.Minute               = <span class="number">60</span> * Second</div><div class="line">time.Hour                 = <span class="number">60</span> * Minute</div></pre></td></tr></table></figure>
<p>这样在计算的时候会很方便,比如打印<code>time.Second</code> 你就会得到<code>time.Duration</code>类型(int64)的 <code>1000000000</code> 这个值.</p>
<h3 id="字符串和时间戳"><a href="#字符串和时间戳" class="headerlink" title="字符串和时间戳"></a>字符串和时间戳</h3><h4 id="获取当前时间戳"><a href="#获取当前时间戳" class="headerlink" title="获取当前时间戳"></a>获取当前时间戳</h4><figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">time.Now().Unix()  <span class="comment">//获取int64类型的当前时间戳</span></div></pre></td></tr></table></figure>
<p>time.Now() 函数返回了一个初始化为当前时间的<code>Time</code>结构体,然后<code>Unix()</code>函数将其转化为<code>int64</code>类型的时间戳数字.</p>
<h4 id="通过给定字符串获取时间戳"><a href="#通过给定字符串获取时间戳" class="headerlink" title="通过给定字符串获取时间戳"></a>通过给定字符串获取时间戳</h4><figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">timeLayout := <span class="string">"2006-01-02 15:04:05"</span></div><div class="line">time, err := time.Parse(timeLayout, <span class="string">"2016-10-10 12:22:12"</span>)</div><div class="line"><span class="keyword">if</span> err == <span class="literal">nil</span> &#123;</div><div class="line">	fmt.Print(time.Unix())</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>注意<code>timeLayout</code>的长度要和给定的字符串长度一样,否则转换会出错.</p>
<h4 id="通过给定时间戳获取时间字符串"><a href="#通过给定时间戳获取时间字符串" class="headerlink" title="通过给定时间戳获取时间字符串"></a>通过给定时间戳获取时间字符串</h4><figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">fmt.Print(time.Unix(<span class="number">1476112209</span>, <span class="number">0</span>).Format(time.ANSIC))</div><div class="line"><span class="comment">//输出Mon Oct 10 23:10:09 2016</span></div></pre></td></tr></table></figure>
<p>time.Unix() 函数通过传入时间戳来获取一个<code>time</code>类型的结构体,然后<code>Format()</code>成指定格式的字符串<br><strong>注意函数time.Unix(sec int64, nsec int64) 和 结构体方法time.Now().Unix() 两个的区别</strong></p>
<h3 id="程序暂停一段时间"><a href="#程序暂停一段时间" class="headerlink" title="程序暂停一段时间"></a>程序暂停一段时间</h3><figure class="highlight golang"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">time.sleep(time.Sleep(time.Second * <span class="number">3</span>)) <span class="comment">//程序暂停3秒</span></div></pre></td></tr></table></figure>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>Author: </span>
      <a href="http://notes.wen.moe">zakokun</a>
    </p>
    <p class="copyright-item">
      <span>Link: </span>
      <a href="http://notes.wen.moe/post/golang-time-package.html">http://notes.wen.moe/post/golang-time-package.html</a>
    </p>
    <p class="copyright-item">
      <span>License: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/golang/">golang</a>
            
              <a href="/tags/技术/">技术</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/post/php-session-bug.html">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">php 中 session_name 不能有句号的问题</span>
        <span class="prev-text nav-mobile">Prev</span>
      </a>
    
    
      <a class="next" href="/post/linux-pressure-testing-tool.html">
        <span class="next-text nav-default">使用 http_load 测试服务器的负载</span>
        <span class="prev-text nav-mobile">Next</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:me@wen.moe" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://www.instagram.com/zakokun" class="iconfont icon-instagram" title="instagram"></a>
        
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    Powered by <a class="hexo-link" href="https://hexo.io/">Hexo</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Theme -
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    Hosted by <a class="theme-link" href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
  </span>

  <span class="copyright-year">
    
    &copy;
    
      2014 -
    
    2017

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">zakokun</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'http://notes.wen.moe/post/golang-time-package.html';
        this.page.identifier = 'post/golang-time-package.html';
        this.page.title = 'phper 学 golang --日期时间处理';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//evanszhang.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>




    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>

  </body>
</html>
